//
//  GQDistributeSelecteLabelController.m
//  GuQu
//
//  Created by HELIUM STAR on 15/5/14.
//  Copyright (c) 2015年 HELIUM STAR. All rights reserved.
//

#import "GQDistributeSelecteLabelController.h"
#define flag @"flag"
#define kSelectMargin 15

@interface GQDistributeSelecteLabelController () <UITableViewDataSource, UITableViewDelegate>

@property (strong, nonatomic) NSArray *headerTitles;
@property (strong, nonatomic) NSMutableArray *arrM;
@property (strong, nonatomic) NSMutableArray *selectedLable;
@property (weak, nonatomic) UITableView *tableView;
@property (assign, getter=isSelect, nonatomic) BOOL select;

@end

@implementation GQDistributeSelecteLabelController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageWithOriginalName:@"home_close"] style:UIBarButtonItemStylePlain target:self action:@selector(cancelToPre)];
    self.title = @"标签选择";
    
    [self setUpTableView];
}

- (void)cancelToPre
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)setUpTableView
{
    UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 20, self.view.width, self.view.height - kNavigatationBarHeight) style:UITableViewStyleGrouped];
    tableView.delegate = self;
    tableView.dataSource = self;
    self.tableView = tableView;
    [self.view addSubview:tableView];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:nil];
    NSInteger count = [self.arrM[indexPath.section] count];
    NSInteger rows = 4;
    CGFloat width = 70;
    CGFloat height = 20;
    CGFloat marginY = 12;
    CGFloat margin = (self.view.width - 2 * kSelectMargin - 4 * width) / (rows - 1);
    for (int i = 0; i < count; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.x = kSelectMargin + i % rows * (width + margin);
        btn.y = marginY + i / rows * (height + marginY);
        btn.width = width;
        btn.height = height;
        btn.tag = i;
        [btn setTitle:self.arrM[indexPath.section][i] forState:UIControlStateNormal];
        btn.titleLabel.font = [UIFont systemFontOfSize:12];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        if ([self.arrM[2] containsObject:btn.titleLabel.text] && indexPath.section == 1) {
            btn.backgroundColor = [UIColor colorWithRed:26/255.0 green:198/255.0 blue:178/255.0 alpha:1.0];

        }else{
            btn.backgroundColor = [UIColor whiteColor];
        }
        if (indexPath.section == 1) {
            [btn addTarget:self action:@selector(btnDidClick:) forControlEvents:UIControlEventTouchUpInside];
        }
        btn.layer.borderColor = [UIColor lightGrayColor].CGColor;
        btn.layer.borderWidth = 1;
        btn.layer.masksToBounds = YES;
        btn.layer.cornerRadius = 10;
        [cell addSubview:btn];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}


- (void)btnDidClick:(UIButton *)btn
{
    if ([self.arrM[2] count] > 1 && ![self.selectedLable containsObject:btn.titleLabel.text]) {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:nil message:@"最多只能选两个标签" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alertView show];
        return;
    }
    
    if (![self.selectedLable containsObject:btn.titleLabel.text]) {
        
        [self.arrM[2] addObject:btn.titleLabel.text];
        [btn removeFromSuperview];
        [self.tableView reloadData];
           }else{
        btn.backgroundColor = [UIColor whiteColor];
        [self.arrM[2] removeObject:btn.titleLabel.text];
        
        [self.tableView reloadData];
    }
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return self.headerTitles[section];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSInteger count = [self.arrM[indexPath.section] count];
    NSInteger clos = count / 4;
    return 12 + (20 + 12) + (20 + 12) * clos;
}

- (NSMutableArray *)arrM
{
    if (_arrM == nil) {
        _arrM = [NSMutableArray array];
        NSArray *arr1 = [NSArray arrayWithObjects:@"字画书法", @"玉翠珠宝", @"交易买卖", nil];
        NSArray *arr2 = [NSArray arrayWithObjects:@"字画书法", @"玉翠珠宝", @"交易买卖", @"如来神掌", @"玉树临风", @"风流倜傥", @"浪遏飞舟", @"指点江山", @"激昂文字", @"意气风发", @"百里放养", @"心如止水",@"放荡不羁", nil];
        NSMutableArray *arr3 = [NSMutableArray array];
        self.selectedLable = arr3;
        [_arrM addObject:arr1];
        [_arrM addObject:arr2];
        [_arrM addObject:arr3];
    }
    return _arrM;
}

- (NSArray *)headerTitles
{
    if (_headerTitles == nil) {
        _headerTitles = [NSArray arrayWithObjects:@"我的标签", @"全部标签", @"已选标签", nil];
    }
    return _headerTitles;
}

@end
